package com.ellison.springdemo.restTest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName RestTemplateTest
 * @Description <p>TODO
 * @Author Ellison_Pei
 * @Date 2022/9/27 09:28
 * @Since 1.0
 **/
@Service
public class RestTemplateService {
    private final Logger logger = LoggerFactory.getLogger(RestTemplateService.class);
    @Resource
    private RestTemplate restTemplate;

    private String FEI_SHU_WEBHOOK_URL = "https://open.feishu.cn/open-apis/bot/v2/hook/3476dc15-2682-4dab-b20b-753cfa0dec65";


    public void send(String textMsg) throws Exception {
        FeishuResponseVo result = restTemplate.postForObject(FEI_SHU_WEBHOOK_URL, buildBody(textMsg), FeishuResponseVo.class);
        assert result != null;
        logger.info("飞书发送结果：{}", result);
    }

    private RobotVo buildBody(String textMsg) {
        RobotVoZhCnContent tagText = new RobotVoZhCnContent();
        tagText.setTag("text");
        tagText.setText(textMsg);

        RobotVoZhCnContent aText = new RobotVoZhCnContent();
        aText.setTag("a");
        aText.setText("请查看\r\n");
        aText.setHref("http://172.29.22.154:8079/main");

        RobotVoZhCnContent atText = new RobotVoZhCnContent();
        atText.setTag("at");
        atText.setUser_id("all");
        atText.setUser_name("所有人");

        List<List<RobotVoZhCnContent>> content = new ArrayList<>();
        content.add(Arrays.asList(tagText, aText, atText));

        RobotVoZhCn zhCn = new RobotVoZhCn();
        String ALARM_PREFIX = "【应用报警】";
        String ALARM_TITLE = "pinpoint监控到报警信息！";
        zhCn.setTitle(ALARM_PREFIX + ALARM_TITLE);
        zhCn.setContent(content);

        RobotVoContentRoot robotVoContentRoot = new RobotVoContentRoot();
        robotVoContentRoot.setPost(new RobotVoContentRootPost(zhCn));

        return new RobotVo("post", robotVoContentRoot);
    }

}
